package controller;

import dao.UserMapper;
import entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.List;

public class Test {
    @org.junit.Test
    public void test(){
        System.out.println("测试开始");
        try {
            //使用MyBatis提供的Resources类加载mybatis的配置文件
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            //构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

            SqlSession session= sessionFactory.openSession();

            UserMapper mapper=session.getMapper(UserMapper.class);

            List<User> list= mapper.getAll();
            for (entity.User arr: list) {
                System.out.println("user_name："+arr.getUsername()+";password："+arr.getPassword());
            }
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}